<?php

class DbKooltaoModel extends DbMysqli {

    protected $host = MYSQLHOST;

    protected $port = MYSQLPORT;

    protected $user = MYSQLUSER;

    protected $passwd = MYSQLPASS;

    protected $db = DB_KOOLTAO;

    protected $link_name = 'kooltao';

    function __construct ($name = null) {
        parent::__construct();
        $name && $this->setTableName($name);
    }

    function _createTable () {
        $sql = "
CREATE TABLE IF NOT EXISTS `kooltao_product`(
  `pid` INT(11) unsigned NOT NULL auto_increment,
  `source_id`  VARCHAR(50) NOT NULL DEFAULT '',
  `title` VARCHAR(256) NOT NULL DEFAULT '',
  `description` LONGTEXT,
  `nick` VARCHAR(256) NOT NULL DEFAULT '',
  `sales`  INT(11) unsigned NOT NULL DEFAULT 0,
  `url` VARCHAR(1000) NOT NULL DEFAULT '',
  `shop_url` VARCHAR(1000) NOT NULL DEFAULT '',
  `price` FLOAT NOT NULL DEFAULT 0,
  `discount` FLOAT NOT NULL DEFAULT 0,
  `fav` INT(11) NOT NULL DEFAULT 0,
  `status` INT(11) NOT NULL DEFAULT 0,
  `uid` INT(11) NOT NULL DEFAULT 0,
  `username` VARCHAR(256) NOT NULL DEFAULT '',
  `created` INT(11) NOT NULL DEFAULT 0,
  `changed` INT(11) NOT NULL DEFAULT 0,
  PRIMARY KEY `pid` (`pid`),
  INDEX `source_id` (`source_id`),
  KEY `nick` (`nick`),
  INDEX `sales` (`sales`),
  INDEX `price` (`price`),
  INDEX `fav` (`fav`),
  INDEX `uid` (`uid`),
  INDEX `created` (`created`),
  INDEX `changed` (`changed`)
)ENGINE=MyISAM DEFAULT CHARSET=utf8
		";
        $this->query($sql);
        
        $sql = "
		CREATE TABLE IF NOT EXISTS `kooltao_vocabulary`(
		`vid` INT(11) unsigned NOT NULL auto_increment,
		`name` VARCHAR(256) NOT NULL DEFAULT '',
		`machine_name` VARCHAR(256) NOT NULL DEFAULT '',
		`description` TEXT,
		`weight` INT(11) NOT NULL DEFAULT 0,
		PRIMARY KEY `vid` (`vid`),
		UNIQUE `machine_name` (`machine_name`),
		INDEX `weight` (`weight`)
		)ENGINE=MyISAM DEFAULT CHARSET=utf8;
		";
        $this->query($sql);
        
        $sql = "
		CREATE TABLE IF NOT EXISTS `kooltao_term`(
		`tid` INT(11) unsigned NOT NULL auto_increment,
		`vid` INT(11) NOT NULL DEFAULT 0,
		`parent` INT(11) NOT NULL DEFAULT 0,
		`name` VARCHAR(256) NOT NULL DEFAULT '',
		`description` TEXT,
		`weight` INT(11) NOT NULL DEFAULT 0,
		PRIMARY KEY `tid` (`tid`),
		INDEX `vid` (`vid`),
		INDEX `parent` (`parent`),
		INDEX `weight` (`weight`),
		INDEX `name` (`name`)
		)ENGINE=MyISAM DEFAULT CHARSET=utf8
		";
        $this->query($sql);
        
        $sql = "
		CREATE TABLE IF NOT EXISTS `kooltao_term_data`(
		`tid` INT(11) NOT NULL DEFAULT 0,
		`vid` INT(11) NOT NULL DEFAULT 0,
		`pid` INT(11) NOT NULL DEFAULT 0,
		INDEX `tid` (`tid`),
		INDEX `vid` (`vid`),
		INDEX `pid` (`pid`)
		)ENGINE=MyISAM DEFAULT CHARSET=utf8
		";
        $this->query($sql);
        
        $sql = "
		CREATE TABLE IF NOT EXISTS `kooltao_user`(
		`uid` INT(11) unsigned NOT NULL auto_increment,
		`name` VARCHAR(60) NOT NULL DEFAULT '',
		`pass` VARCHAR(128) NOT NULL DEFAULT '',
		`email` VARCHAR(256) NOT NULL DEFAULT '',
		`alipay_account` VARCHAR(256) NOT NULL DEFAULT '',
		`nick` VARCHAR(256) NOT NULL DEFAULT '',
		`status` INT(11) NOT NULL DEFAULT 0,
		`register_ip` INT(11) NOT NULL DEFAULT 0,
		`last_login_time` INT(11) NOT NULL DEFAULT 0,
		`last_login_ip` INT(11) NOT NULL DEFAULT 0,
		`created` INT(11) NOT NULL DEFAULT 0,
		`role` INT(11) NOT NULL DEFAULT 0,
		PRIMARY KEY `uid` (`uid`),
		UNIQUE `name` (`name`),
		UNIQUE `email` (`email`)
		)ENGINE=MyISAM DEFAULT CHARSET=utf8
		";
        
        $this->query($sql);
        
        $this->setTableName('kooltao_user');
        $this->insert(array('name' => 'admin', 'pass' => md5('admin@kooltao#CP$' . SALT), 'role'=>1));
        
        $sql = "
		CREATE TABLE IF NOT EXISTS `kooltao_role`(
		`rid` INT(11) unsigned NOT NULL auto_increment,
		`name` VARCHAR(128) NOT NULL DEFAULT '',
		`machine_name` VARCHAR(128) NOT NULL DEFAULT '',
		`description` VARCHAR(256) NOT NULL DEFAULT '',
		PRIMARY KEY `rid` (`rid`),
		INDEX `machine_name` (`machine_name`)
		)ENGINE=MyISAM DEFAULT CHARSET=utf8
		";
        
        $this->query($sql);
        
        $sql = "
		CREATE TABLE IF NOT EXISTS `kooltao_favorite`(
		`fid` INT(11) unsigned NOT NULL auto_increment,
		`uid` INT(11) NOT NULL DEFAULT 0,
		`pid` INT(11) NOT NULL DEFAULT 0,
		PRIMARY KEY `fid` (`fid`),
		UNIQUE `uid` (`uid`),
		UNIQUE `pid` (`pid`)
		)ENGINE=MyISAM DEFAULT CHARSET=utf8
		";
        
        $this->query($sql);
        
        $sql = "
        CREATE TABLE IF NOT EXISTS `kooltao_images`(
        `image_id` INT(11) unsigned NOT NULL auto_increment,
        `uid` INT(11) NOT NULL DEFAULT 0,
        `pid` INT(11) NOT NULL DEFAULT 0,
        `type` VARCHAR(256) NOT NULL DEFAULT '',
        `imagename` VARCHAR(256) NOT NULL DEFAULT '',
        `path` VARCHAR(512) NOT NULL DEFAULT '',
        `filesize` INT(11) NOT NULL DEFAULT 0,
        `width` INT(11) NOT NULL DEFAULT 0,
        `height` INT(11) NOT NULL DEFAULT 0,
        `data` TEXT,
        `created` INT(11) NOT NULL DEFAULT 0,
        PRIMARY KEY `image_id` (`image_id`),
        INDEX `uid` (`uid`),
        INDEX `pid` (`pid`)
        )ENGINE=MyISAM DEFAULT CHARSET=utf8
        ";
        
        $this->query($sql);
    }
}